
/**
 * {
 *   mixins: [
 *    dataSource('chartData', '/ibps/information/industryDevelopmentResearchIndustry/getChart', {
 *      transformData: transformG2plotFormRemoteData('result.meta', {
            'result.char.100': {
                name: 'chart1',
                label: {field: 'limitTrendTime'},
                value: {field: 'oneHundredCity'},
                value2: {field: 'sequentialChange'},
            }

        })
 *    })
 * ]
 * }
 * 
 * @author xiufu.wang
 */

import { objectProperty } from 'mars-pro/src/pro/util'
import omit from 'mars-pro/src/pro/omit'
import moment from 'moment'
import numberFormater from 'mars-pro/src/pro/number-format-v2'

// %
function valueTransFormerForPer(value) {
    if (isNaN(value) || value === null) {
        return value
    }
    return numberFormater(value, 0.001, false, 2)
}

// 万
function valueTransFormerForWan(value) {
    if (isNaN(value) || value === null) {
        return value
    }
    return numberFormater(value, 10000, false, 2)
}

// 亿
function valueTransFormerForYi(value) {
    if (isNaN(value) || value === null) {
        return value
    }
    return numberFormater(value, 100000000, false, 2)
}

// 百万
function valueTransFormerForBw(value) {
    if (isNaN(value) || value === null) {
        return value
    }
    return numberFormater(value, 1000000, false, 2)
}

// 年
function valueTransFormerForYear(value) {
    return moment(value).format('YYYY')
}

// 月
function valueTransFormerForMonth(value) {
    return moment(value).format('YYYY-MM')
}


// 日
function valueTransFormerForDay(value) {
    return moment(value).format('YYYY-MM-DD')
}

const valueTransFormersSort = [
    ['日', valueTransFormerForDay],
    ['月', valueTransFormerForMonth],
    ['年', valueTransFormerForYear],
    ['百万', valueTransFormerForBw],
    ['万', valueTransFormerForWan],
    ['亿', valueTransFormerForYi],
    ['%', valueTransFormerForPer],
]

function resolveTransFormFn(alias) {
    const tag = alias.slice(alias.lastIndexOf('(') + 1, alias.length - 1)
    for (let i = 0; i < valueTransFormersSort.length; i++) {
        const r = valueTransFormersSort[i];
        if (tag.indexOf(r[0]) > -1) {
            return r[1]
        }
    }
    return null
}

function genChartMetaData(chartMeta) {
    let alias = chartMeta.alias || ''
    alias = alias.trim()
    if (!alias) {
        return
    }
    chartMeta.formatter = resolveTransFormFn(alias)
}

function genChartData(metaDef, valueOpt, data, labelOpt, type) {
    const _dataMetaInfo = {
        label: {
            alias: metaDef[labelOpt.field]
        }
    }

    var res = []
    Object.keys(valueOpt).forEach((valueField) => {
        const opts = Array.isArray(valueOpt[valueField]) ? valueOpt[valueField] : [valueOpt[valueField]]
        const vdata = (data || []).reduce((m, v) => {
            for (let i = 0; i < opts.length; i++) {
                const o = opts[i];
                m.push({
                    [valueField]: objectProperty(v, o.field, null),
                    type: (o.type || metaDef[o.field]),
                    label: objectProperty(v, labelOpt.field),
                    _record_: v
                })
                _dataMetaInfo[valueField] = _dataMetaInfo[valueField] || { alias: (o.alias || metaDef[o.field]) }
            }
            return m
        }, [])
        res.push(vdata)
    })

    //生成meta配置
    Object.keys(_dataMetaInfo).forEach((r) => {
        genChartMetaData(_dataMetaInfo[r])
    })

    return {
        chartData: res.length > 1 ? res : res[0],
        chartMeta: _dataMetaInfo
    }
}

/**
 * 
 * @param {*} metaField 
 * @param {*} option 
 * @returns 
 */
export default function transformG2plotFormRemoteData(metaField, option, type) {
    return (data) => {
        let result = {}
        const metaDef = objectProperty(data, metaField, {})
        const valuesOpt = omit(option, ['label'])

        Object.keys(option).forEach((dataField) => {
            const _opt = option[dataField]
            const valueOpt = omit(_opt, ['label', 'name'])
            const labelOpt = _opt.label
            const _data = objectProperty(data, dataField, {})
            result[_opt.name] = genChartData(metaDef, valueOpt, _data, labelOpt, type)
        })
        return result
    }
}
